<?php
Class IndexController{
public $title;
public $index;
public $link;
public $page;
public $page_id;
public $type;

	public function __construct(){
		LoadHelpers("Use");
		
	}

	public function IndexAction(){
		$request = getRequest::getInstance();
		$result = $request->Uri();
		if(!empty($result)){
                       $url = "\"/".implode('/', $result).".html\"";
                       $where = "`url`=".$url;
                       $count = sizeof($result);
							switch($count){
								case("1"):
									if($result['0']=='index'){
										$this->index = 1;
									}
								break;
								case("2"):
									if($result['0']=='index' && $result['1']=='index'){
										$this->index = 1;
									}
								break;
							}
		}else{
			$this->index = 1;
			$where = "`index` = '1'";
		}
		if(isset($this->index) && $this->index==1){
			$title = "";
			$keywords = NULL;
            $description = NULL;
            
            //$where = array("content.razdel" => "razdel.id", "razdel.published"=>1, "razdel.id"=>63);
			//$this->block =  ORM::factory()->Select("content.razdel, content.body")->Where($where)->OrderBy("sort","ASC")->crossjoin('razdel, content')->find_all();
			/*echo"<pre>";
			print_r($this->block);
			echo"</pre>";*/
			$this->title = "Головна";
		}else{					
					$inf = ORM::factory()->SetType('assoc')->prepare("SELECT id,razdel,type,published,title,keywords,description,url FROM `razdel` WHERE ".$where)->getResult();

					if(isset($inf['0']) && $inf['0']['published']==1){
                                    $id = $inf['0']['id'];
                                    $this->title = $inf['0']['title'];
                                    $this->page_id = $inf['0']['id'];
									$this->page_razdel = $inf['0']['razdel'];
                                    $array = array(
										array('link'=>'index','name'=>'Головна'),	
									);
									
                                    $keywords = $inf['0']['keywords'];
                                    $description = $inf['0']['description'];
									
                                    $this->type = $inf['0']['type'];
                                                
									if(isset($this->index) && $this->index!=1 || !isset($this->index)){
                                    switch($this->type){
                                        case(1):
                                            $this->conent = ORM::factory('content')->SetType('assoc')->Where("razdel",$id)->body;
                                        break;
                                        case(2):
											$where = array("razdel.razdel" => $id, "content.razdel" => "razdel.id", "razdel.published"=>1);
											$this->pages =  ORM::factory()->Select("content.razdel, content.descrip")->Where($where)->OrderBy("sort","ASC")->crossjoin('razdel, content')->find_all();
                                        break;
										}
									}
									$array[] = array('link'=>implode('/', $result),'name'=>$this->title);
									$this->link = LinkGen::Link($array, 'block', 'sitemap');				
                    }else{
							if($count==1){
									$goods = ORM::factory()->SetType('assoc')->prepare("SELECT GroupTovar.name as `gr_name`, GroupTovar.url as `gurl`, goods.id, goods.url, goods.name, goods.prev FROM `GroupTovar` LEFT JOIN `goods` ON (goods.Cat = GroupTovar.id) WHERE GroupTovar.url='".$result[0]."' AND goods.Sub = '0'")->getResult();
									if(!empty($goods)){
										$this->type = 5;
										$this->title = $goods[0]['gr_name'].' - '.$goods[0]['c_name'];
										
										$array = array(
											array('link'=>'index','name'=>'Головна'),
											array('link'=>$goods[0]['gurl'], 'name'=>$goods[0]['gr_name'])
										);
										$this->link = LinkGen::Link($array, 'block', 'sitemap');
										
										$this->goods = $goods;
										unset($goods);
									}else{
										$this->conent = "Сторінку не знайдено!";
										$this->title = "Сторінку не знайдено!";
										$this->type = 'error';
									}
							}elseif($count==2){
									$goods = ORM::factory()->SetType('assoc')->prepare("SELECT GroupTovar.name as `gr_name`, CetegoryTovar.name as `c_name`, CetegoryTovar.url as `curl`, GroupTovar.url as `gurl`, goods.id, goods.url, goods.name, goods.prev FROM `GroupTovar`,`CetegoryTovar` LEFT JOIN`goods` ON (goods.Sub = CetegoryTovar.id) WHERE GroupTovar.url='".$result[0]."' AND CetegoryTovar.group=GroupTovar.id AND CetegoryTovar.url = '".$result[1]."'")->getResult();

									if(!empty($goods)){
										$this->type = 6;
										$this->title = $goods[0]['gr_name'].' - '.$goods[0]['c_name'];
										
										$array = array(
											array('link'=>'index','name'=>'Головна'),
											array('name'=>$goods[0]['gr_name']),
											array('link'=>$goods[0]['gurl'].'/'.$goods[0]['curl'],'name'=>$goods[0]['c_name'])
											
										);
										$this->link = LinkGen::Link($array, 'block', 'sitemap');
										
										$this->goods = $goods;
										unset($goods);
									}else{
										$tovar = ORM::factory()->SetType('assoc')->prepare("SELECT goods.id, goods.name, goods.materіal, goods.color, goods.term, goods.home, goods.image, goods.body, goods.descrip, goods.Cat, GroupTovar.name as `gname`, GroupTovar.url as `gurl` FROM `GroupTovar`, `goods` WHERE GroupTovar.url = '".$result[0]."' AND goods.Cat=GroupTovar.id AND goods.url = '".$result[1]."'")->getResult();
										$this->tovar2 = array();
										
										if(!empty($tovar)){
											$this->tovar = $tovar[0];
											$this->type = 7;
											$this->title = $tovar[0]['name'];
											$this->url = "http://donnaliya.com.ua/".$result[0].'/'.$result[1].'.html';
											$conf = Config::Ini(APPLICATION_PATH.'/configs/application.conf', 'config',1);
											$this->soc = $conf['social'];
											unset($conf);

											$this->tovar2 = ORM::factory()->SetType('assoc')->prepare("SELECT `url`,`name`,`prev` FROM `goods` WHERE `Cat`='".$tovar[0]['Cat']."' AND `materіal`='".$tovar[0]['materіal']."' AND `color`='".$tovar[0]['color']."' AND `id`!='".$tovar[0]['id']."' LIMIT 7")->getResult();
											$this->images = ORM::factory('gallery_tov')->SetType('assoc')->Select('id,medi,image,name')->Where('goods',$tovar[0]['id'])->find_all();
											$this->user_info = Df_Auth::read("user");
											
											$this->good_auth = FALSE;
											if(empty($this->user_info)){
												if($request->isPost()){
													$this->layout = 'false';
													
													require_once(PLUGINS_PATH.'/Loginza.php');
													if(Loginza($request)){
														$this->good_auth = TRUE;
														header('Location: '.$this->url);
													}else{
														$this->good_auth = FALSE;
														header('Location: '.$this->url);
													}
												}
											}else{
												$this->good_auth = TRUE;
											}
											
											$this->comments = ORM::factory('comments')->SetType('assoc')->Where('goods',$tovar[0]['id'])->find_all();

											$array = array(
													array('link'=>'index','name'=>'Головна'),
													array('link'=>$tovar[0]['gurl'],'name'=>$tovar[0]['gname']),
													array('link'=>$tovar[0]['gurl'].'/'.$result[1],'name'=>$tovar[0]['name'])
											);
											$this->link = LinkGen::Link($array, 'block', 'sitemap');
										}else{
											$this->conent = "Сторінку не знайдено!";
											$this->title = "Сторінку не знайдено!";
											$this->type = 'error';
										}
									}
							}elseif($count==3){
								$tovar = ORM::factory()->SetType('assoc')->prepare("SELECT goods.id, goods.name, goods.materіal, goods.color, goods.term, goods.home, goods.image, goods.body, goods.descrip, goods.Cat, goods.Sub, GroupTovar.name as `gname`, GroupTovar.url as `gurl`, CetegoryTovar.name as `cname`, CetegoryTovar.url as `curl` FROM `GroupTovar`,`CetegoryTovar`,`goods` WHERE GroupTovar.url = '".$result[0]."' AND GroupTovar.id = CetegoryTovar.group AND CetegoryTovar.url = '".$result[1]."' AND CetegoryTovar.id = goods.Sub AND goods.url = '".$result[2]."'")->getResult();
								$this->tovar2 = array();
								
								if(!empty($tovar)){
									$this->tovar = $tovar[0];
									$this->type = 7;
									$this->title = $tovar[0]['name'];
									$this->url = "http://donnaliya.com.ua/".$result[0].'/'.$result[1].'/'.$result[2].'.html';
									$conf = Config::Ini(APPLICATION_PATH.'/configs/application.conf', 'config',1);
									$this->soc = $conf['social'];
									unset($conf);

									$this->tovar2 = ORM::factory()->SetType('assoc')->prepare("SELECT `url`,`name`,`prev` FROM `goods` WHERE `Cat`='".$tovar[0]['Cat']."' AND `Sub`='".$tovar[0]['Sub']."' AND `materіal`='".$tovar[0]['materіal']."' AND `color`='".$tovar[0]['color']."' AND `id`!='".$tovar[0]['id']."' LIMIT 7")->getResult();
									$this->images = ORM::factory('gallery_tov')->SetType('assoc')->Select('id,medi,image,name')->Where('goods',$tovar[0]['id'])->find_all();
									$this->user_info = Df_Auth::read("user");
									
									$this->good_auth = FALSE;
									if(empty($this->user_info)){
										if($request->isPost()){
											$this->layout = 'false';
											
											require_once(PLUGINS_PATH.'/Loginza.php');
											if(Loginza($request)){
												$this->good_auth = TRUE;
												header('Location: '.$this->url);
											}else{
												$this->good_auth = FALSE;
												header('Location: '.$this->url);
											}
										}
									}else{
										$this->good_auth = TRUE;
									}
									
									$this->comments = ORM::factory('comments')->SetType('assoc')->Where('goods',$tovar[0]['id'])->find_all();

									$array = array(
											array('link'=>'index','name'=>'Головна'),
											array('name'=>$tovar[0]['gname']),
											array('link'=>$tovar[0]['gurl'].'/'.$tovar[0]['curl'],'name'=>$tovar[0]['cname']),
											array('link'=>$tovar[0]['gurl'].'/'.$tovar[0]['curl'].'/'.$result[2],'name'=>$tovar[0]['name'])
									);
									$this->link = LinkGen::Link($array, 'block', 'sitemap');
								}else{
									$this->conent = "Сторінку не знайдено!";
									$this->title = "Сторінку не знайдено!";
									$this->type = 'error';
								}
							}
							
							
                            
                            $keywords = NULL;
                            $description = NULL;
                    }
                    if($this->type == 'error'){
					
					}
		}
		$this->keywords = $keywords;
		$this->description = $description;
        }
}
